package usercentercontroller

import (
	"context"
	"gitee.com/go-mid/booter/bootservice"
	"gitee.com/go-mid/booter/web"
	"gitee.com/go-mid/infra/xlog"
	"gitee.com/go-mid/user/userservice"
	"gitee.com/go-mid/usercenter/usercentercontroller/dapp"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

type UserBaseInfoRequest struct {
} //@name 获取用户请求

func FactoryUserBaseInfoRequest() web.HandleRequest {
	return new(UserBaseInfoRequest)
}

//
// @Summary      获取当前登录用户
// @Description  获取当前登录用户
// @Tags		账号
// @Param        message	body		UserBaseInfoRequest	true	"入参"
// @Success      200  {object}  web.ApiReturn{payload=userservice.GetUserRes} "响应"
// @Router       /user/base-info [post]
func (m *UserBaseInfoRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	fun := "UserController.UserBaseInfo -->"
	userID := authInfo.UserId
	xlog.Infof(ctx, "%s req:%+v", fun, m)
	res, err := userservice.DefaultUserService.GetUser(ctx, &userservice.GetUserReq{
		UserID:  userID,
		ActorID: 0,
	})
	if err != nil {
		xlog.Warnf(ctx, "%s DefaultPointsUserService.UserBaseInfo err: req: %v  err: %v", fun, m, err)
		return nil, status.Error(codes.Internal, err.Error())
	}
	return res, nil
}

type DappNonceRequest struct {
} //@name dapp nonce请求
type DappNonceResponse struct {
	Nonce string `json:"nonce"`
} //@name dapp nonce响应

func FactoryDappNonceRequest() web.HandleRequest {
	return new(DappNonceRequest)
}

//
// @Summary      dapp nonce
// @Description  dapp nonce
// @Tags		账号
// @Param        message	body		DappNonceRequest	true	"入参"
// @Success      200  {object}  web.ApiReturn{payload=DappNonceResponse} "响应"
// @Router       /user/dapp/nonce [post]
func (m *DappNonceRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	fun := "UserController.DappNonceRequest -->"
	nonce, err := dapp.GetNonce(ctx)
	if err != nil {
		xlog.Warnf(ctx, "%s dapp.GetNonce err: req: %v  err: %v", fun, m, err)
		return nil, err
	}
	return &DappNonceResponse{
		Nonce: nonce,
	}, nil
}

type DappLoginRegisterRequest struct {
	userservice.RegisterReq
	//签名信息
	Signature string `json:"signature"`
	Nonce     string `json:"nonce"`
} //@name dapp登录注册请求
type RegisterResponse struct {
	userservice.RegisterRes
} //@name dapp登录响应

func FactoryDappLoginRegisterRequest() web.HandleRequest {
	return new(DappLoginRegisterRequest)
}

//
// @Summary      dapp注册登录
// @Description  dapp注册登录
// @Tags		账号
// @Param        message	body		DappLoginRegisterRequest	true	"入参"
// @Success      200  {object}  web.ApiReturn{payload=RegisterResponse} "响应"
// @Router       /user/dapp/register-login [post]
func (m *DappLoginRegisterRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	fun := "UserController.DappLoginRegisterRequest -->"
	valid, err := dapp.VarifyDappSignature(ctx, m.Nonce, m.Loginid, m.Signature)
	if err != nil {
		xlog.Warnf(ctx, "%s dapp.VarifyDappSignature err: req: %v  err: %v", fun, m, err)
		return nil, err
	}
	if !valid {
		xlog.Warnf(ctx, "%s dapp.VarifyDappSignature fail: req: %v  err: %v", fun, m, err)
		return nil, err
	}
	m.RegisterParam.ClientInfo = clientInfo
	regRes, err := userservice.DefaultUserService.Register(ctx, &userservice.RegisterReq{
		RegisterParam: m.RegisterParam,
		IsLogin:       true,
	})
	if err != nil {
		xlog.Warnf(ctx, "%s userservice.DefaultUserService.Login err: req: %v  err: %v", fun, m, err)
		return nil, err
	}
	//把登录凭证写入到头里
	web.SetAuthentication(ctx, c.Writer, func(ctx2 context.Context) string {
		return regRes.AuthInfo.AccessToken
	})
	return &RegisterResponse{
		RegisterRes: *regRes,
	}, nil
}

type UpsertUserAttrRequest struct {
	userservice.UpsertUserAttrsReq
} //@name dapp登录注册请求
type UpsertUserAttrResponse struct {
	userservice.UpsertUserAttrsRes
} //@name dapp登录响应

func FactoryUpsertUserAttrRequest() web.HandleRequest {
	return new(UpsertUserAttrRequest)
}

//
// @Summary      修改用户属性
// @Description  修改用户属性
// @Tags		账号
// @Param        message	body		UpsertUserAttrRequest	true	"入参"
// @Success      200  {object}  web.ApiReturn{payload=UpsertUserAttrResponse} "响应"
// @Router       /user/attrs/upsert [post]
func (m *UpsertUserAttrRequest) Handle(ctx context.Context, c *bootservice.Context, clientInfo web.XFRMClientInfo, authInfo web.XFRMAuthInfo) (interface{}, error) {
	fun := "UserController.UpsertUserAttrRequest -->"
	m.UpsertUserAttrsReq.UserID = authInfo.UserId
	regRes, err := userservice.DefaultUserService.UpsertUserAttr(ctx, &m.UpsertUserAttrsReq)
	if err != nil {
		xlog.Warnf(ctx, "%s userservice.DefaultUserService.Login err: req: %v  err: %v", fun, m, err)
		return nil, err
	}
	return &UpsertUserAttrResponse{
		UpsertUserAttrsRes: *regRes,
	}, nil
}
